package com.ss.baseinfo.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ss.baseinfo.pojo.CourseInfo;
import com.ss.baseinfo.service.CourseService;
import com.ss.common.util.JsonUtil;
import com.ss.common.util.MyToolUtil;

/**
 * 课程管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("course/")
public class CourseController {
	@Autowired
	private CourseService service;
	//新增
	@RequestMapping("/save.do")
	public void save(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		String action=MyToolUtil.convertNull(request.getParameter("action"));
		String id=MyToolUtil.convertNull(request.getParameter("id"));
		String name=MyToolUtil.convertNull(request.getParameter("name"));
		
		CourseInfo info = new CourseInfo();
		info.setName(name);
		String msg="";	
		if("add".equals(action)){
			service.add(info);
			msg="保存成功";
		}else{
			info.setId(id);
			boolean ret=service.update(info);
			if(ret){
				msg="保存成功";
			}else{
				msg="保存失败";
			}
		}
		mapResult.put("msg", msg);
		JsonUtil.outputJson(mapResult, response);
	}
	//调整到编辑页面
	@RequestMapping("/toEdit.do")
	public ModelAndView toEdit(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		String id=MyToolUtil.convertNull(request.getParameter("id"));
		if(!id.equals("")){
			CourseInfo info = new CourseInfo();
			info=service.getOneById(id);
			mapResult.put("info", info);
		}
		return new ModelAndView("/baseinfo/courseEdit",mapResult);
	}
	//保存
	@RequestMapping("/query.do")
	public ModelAndView query(HttpServletRequest request, 
	           HttpServletResponse response) throws Exception{
		Map<String, Object> mapResult=new HashMap<String, Object>();
		CourseInfo info = new CourseInfo();
		List<CourseInfo> list= service.query(info);
		mapResult.put("list", list);
		return new ModelAndView("baseinfo/courseManage",mapResult);
	}
}
